package permission

import (
	"context"

	"mall/common/errorx"
	"mall/common/jwtx"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/rpc/backendclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type SetPermissionStatusLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSetPermissionStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetPermissionStatusLogic {
	return &SetPermissionStatusLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SetPermissionStatusLogic) SetPermissionStatus(req *types.SetPermissionStatusRequest) (resp *types.SetPermissionStatusResponse, err error) {
	if len(req.Ids) == 0 {
		return nil, errorx.Error(errorx.ERROR_PERM_ID_REQUIRED, "")
	}

	if req.Status != 0 && req.Status != 1 {
		return nil, errorx.Error(errorx.ERROR_PERM_STATUS_INVALID, "")
	}

	adminId, _ := jwtx.GetUserId(l.ctx)

	_, err = l.svcCtx.BackendRpc.SetPermissionStatus(l.ctx, &backendclient.SetPermissionStatusRequest{
		Ids:       req.Ids,
		Status:    req.Status,
		UpdatedBy: adminId,
	})
	if err != nil {
		return nil, err
	}

	return &types.SetPermissionStatusResponse{
		Status:  "ok",
		Message: "",
	}, nil
}
